<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="24" class="text-right">
                <el-radio-group v-model="form.type" @change="onSelectType">
                    <el-radio-button label="企业证书" :value="1" />
                    <el-radio-button label="人员证书" :value="2" />
                    <el-radio-button label="技工证书" :value="3" />
                    <el-radio-button label="专利证书" :value="4" />
                </el-radio-group>
                <el-input class="ml15" v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        <el-table v-if="form.type == 1" class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column label="名称" width="200">
                <template #default="scope">
                    <el-text>{{ scope.row.company }}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="title" label="资质类型" />
            <el-table-column prop="licence" label="发证机关" />
            <el-table-column prop="numbers" label="证书编号" />
            <el-table-column prop="aptitude" label="资质类别" />
            <el-table-column prop="start_date" label="有效期起" />
            <el-table-column label="有效期至" width="180">
                <template #default="scope">
                    <el-text >{{ scope.row.date_type != 1 ? scope.row.end_date : '长期' }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="查看" width="50">
                <template #default="scope">
                    <el-link type="primary" @click="onShow(scope.row.files)">查看</el-link>
                </template>
            </el-table-column>
            <el-table-column label="完成时间" width="180">
                <template #default="scope">
                    <el-text v-if="scope.row.files">{{scope.row.updated_at}}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="remarks" label="备注" />
            <!-- <el-table-column label="下载记录" width="90">
                <template #default="scope">
                    <el-link type="primary" @click="onShowDown(scope.row.downs)">查看</el-link>
                </template>
            </el-table-column> -->
            <el-table-column label="申请状态" show-overflow-tooltip>
                <template #default="scope">
                    <el-text type="primary" v-if="scope.row.application" @click="onApplyRecord(scope.row)" class="cp">{{scope.row.application.status_text}}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160">
                <template #default="scope">
                    <el-link  type="primary" @click="onApply(scope.row.id)" >下载申请</el-link>
                </template>
            </el-table-column>
        </el-table>

        <el-table v-if="form.type == 2" class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column label="姓名" width="120">
                <template #default="scope">
                    <el-text v-if="scope.row.user">{{ scope.row.user.name }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="身份证件号" width="200">
                <template #default="scope">
                    <el-text v-if="scope.row.user">{{ scope.row.user.idcard }}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="title" label="证书类别" />
            <el-table-column prop="numbers" label="证书编号" width="300" />
            <el-table-column prop="start_date" label="有效期起" width="300" />
            <el-table-column label="失效日期" width="180">
                <template #default="scope">
                    <el-text >{{ scope.row.date_type != 1 ? scope.row.end_date : '长期' }}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="查看" width="50">
                <template #default="scope">
                    <el-link type="primary" @click="onShow(scope.row.files)">查看</el-link>
                </template>
            </el-table-column>
            <el-table-column label="完成时间" width="180">
                <template #default="scope">
                    <el-text v-if="scope.row.files">{{scope.row.updated_at}}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="remarks" label="备注" />
            <el-table-column label="申请状态" show-overflow-tooltip>
                <template #default="scope">
                    <el-text type="primary" v-if="scope.row.application" @click="onApplyRecord(scope.row)" class="cp">{{scope.row.application.status_text}}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160">
                <template #default="scope">
                    <el-link  type="primary" @click="onApply(scope.row.id)" >下载申请</el-link>
                </template>
            </el-table-column>
        </el-table>


        <el-table v-if="form.type == 3" class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column label="姓名" width="120">
                <template #default="scope">
                    <el-text v-if="scope.row.user">{{ scope.row.user.name }}</el-text>
                </template>
            </el-table-column>
            <el-table-column label="身份证件号" width="200">
                <template #default="scope">
                    <el-text v-if="scope.row.user">{{ scope.row.user.idcard }}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="title" label="证书类别" />
            <el-table-column prop="numbers" label="证书编号" width="300" />
            <el-table-column prop="start_date" label="有效期起" width="300" />
            <el-table-column label="失效日期" width="180">
                <template #default="scope">
                    <el-text >{{ scope.row.date_type != 1 ? scope.row.end_date : '长期' }}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="查看" width="50">
                <template #default="scope">
                    <el-link type="primary" @click="onShow(scope.row.files)">查看</el-link>
                </template>
            </el-table-column>
            <el-table-column label="完成时间" width="180">
                <template #default="scope">
                    <el-text v-if="scope.row.files">{{scope.row.updated_at}}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="remarks" label="备注" />
            <el-table-column label="申请状态" show-overflow-tooltip>
                <template #default="scope">
                    <el-text type="primary" v-if="scope.row.application" @click="onApplyRecord(scope.row)" class="cp">{{scope.row.application.status_text}}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160">
                <template #default="scope">
                    <el-link  type="primary" @click="onApply(scope.row.id)" >下载申请</el-link>
                </template>
            </el-table-column>
        </el-table>

        <el-table v-if="form.type == 4" class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column prop="title" label="专利名称" />
            <el-table-column prop="aptitude" label="专利类型" />
            <el-table-column prop="acquired_at" label="授予时间" />
            <el-table-column prop="numbers" label="专利号" width="300" />
            <el-table-column prop="nationality" label="国别" />
            <el-table-column prop="inventor" label="发明人" />
            <el-table-column label="有效期至" width="180">
                <template #default="scope">
                    <el-text >{{ scope.row.date_type != 1 ? scope.row.end_date : '长期' }}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="查看" width="50">
                <template #default="scope">
                    <el-link type="primary" @click="onShow(scope.row.files)">查看</el-link>
                </template>
            </el-table-column>
            <el-table-column label="完成时间" width="180">
                <template #default="scope">
                    <el-text v-if="scope.row.files">{{scope.row.updated_at}}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="patent_owner" label="专利权所有人" />
            <el-table-column prop="remarks" label="备注" />
            <el-table-column label="申请状态" show-overflow-tooltip>
                <template #default="scope">
                    <el-text type="primary" v-if="scope.row.application" @click="onApplyRecord(scope.row)" class="cp">{{scope.row.application.status_text}}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160">
                <template #default="scope">
                    <el-link  type="primary" @click="onApply(scope.row.id)" >下载申请</el-link>
                </template>
            </el-table-column>
        </el-table>


        <!-- <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" />
            <el-table-column prop="title" label="证书" show-overflow-tooltip />
            <el-table-column prop="numbers" label="证书编号" show-overflow-tooltip />
            <el-table-column prop="type_text" label="类型" show-overflow-tooltip />
            <el-table-column prop="start_date" label="生效日期" width="180" />
            <el-table-column prop="end_date" label="失效日期" width="180" />
            <el-table-column fixed="right" label="文件" width="80">
                <template #default="scope">
                    <el-link type="primary" @click="onShow(scope.row.files)">查看</el-link>
                </template>
            </el-table-column>
            <el-table-column label="申请状态" show-overflow-tooltip>
                <template #default="scope">
                    <el-text type="primary" v-if="scope.row.application" @click="onApplyRecord(scope.row)" class="cp">{{scope.row.application.status_text}}</el-text>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160">
                <template #default="scope">
                    <el-link  type="primary" @click="onApply(scope.row.id)" >下载申请</el-link>
                </template>
            </el-table-column>
        </el-table> -->
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

    <el-dialog v-model="isFile" title="证书查看" width="60%">
        <el-image style="width: 100%" :src="files" fit="contain" />
    </el-dialog>

    <el-dialog v-model="isApply" title="下载证书" width="80%">
        <!-- <component :is="certForm" :id = editId type = 'apply' model = 'Cert' @onApplySave="onApplySave" /> -->
        <component :is="certForm" :data = editId :type="false" @onSave="onSave" />
    </el-dialog>

    <el-dialog v-model="isApplyRecord" title="申请进度" width="50%">
        <component :is="applyRecord" :id = editId />
    </el-dialog>

</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import {  RefreshRight, Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
import certForm from "@/components/certForm.vue";
import applyRecord from "@/components/applyRecord.vue";
const indexMethod = (index: number) => {
  return index+= 1
}
const isApply = ref(false)
const isApplyRecord = ref(false)
const files = ref('')
const isFile = ref<boolean>(false)
const editId = ref(0)
const form = ref<any>({search:'',type:1,page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
interface Cert {
    id: number
    title: string
    numbers: string
    type: number
    start_date: string
    end_date: string
    files: string
}
const tableData = ref<Cert[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/cert',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onSelectType = () => {
    form.value.page = 1
    onGetData()
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    form.value.page = 1
    onGetData()
}

const onApply = (id: number) => {
    editId.value = id
    isApply.value = true
}

const onShow = (data: string) => {
    files.value = data+'?imageMogr2/thumbnail/!50p'
    isFile.value = true
}

const onApplyRecord = (data: any) => {
    editId.value = data.application.id
    isApplyRecord.value = true
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
}
onMounted(()=>{
    onGetData()
})


const onSave = () => {
    isApply.value = false
    onSave()
}
</script>

<style scoped>
.el-tag{margin: 15px;}
</style>